package com.resou.reader.activity.freebook;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.service.ActivityService;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.data.Injection;
import com.resou.reader.data.bookstore.BookStoreRepository;
import com.resou.reader.data.bookstore.model.MorePlateInfo;
import com.resou.reader.data.bookstore.response.PlateInfoResponse;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.utils.ToastUtil;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class FreeBookPresenter extends ResouBasePresenter<FreeBookContract.View> implements FreeBookContract.Presenter {
    private static final String TAG = "FreeBookPresenter";
    private final BookStoreRepository mRepository;
    private final ActivityService mService;

    public FreeBookPresenter(FreeBookContract.View view) {
        super(view);
        mRepository = Injection.provideBookstoreRepository();
        mService = ApiImp.getInstance().getService(ActivityService.class);
    }

    @Override
    public void loadData(String gender) {
        addCompositeDisposable(
                mRepository.getMoreInfo(gender, "32", 0, 6)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(responseResult -> {
                                    if (responseResult.getCode() == 0) {
                                        PlateInfoResponse<MorePlateInfo> resultData = responseResult.getData();
                                        List<MorePlateInfo> plateInfo = resultData.getPlateInfo();
                                        mView.setData(plateInfo);
                                        mView.showContent();
                                    } else {
                                        ToastUtil.makeShortToast(responseResult.getMsg());
                                    }
                                }, throwable -> {
                                    mView.showError(throwable);
                                }
                        )
        );
    }

    @Override
    public void startFifteenFreeActivity() {
        addCompositeDisposable(
                mService.startFifteenDays(UserInstance.getToken())
                        .subscribeOn(getIOSchedulers())
                        .observeOn(getMainSchedulers())
                        .subscribe(result -> {
                            if (result.getCode() == 0) {
                                String data = result.getData();
                            }

                        }, throwable -> {

                        })
        );
    }
}
